import { fetchList } from "./index";

import {
  drawerFormState,
  operateType,
  paymentMerchantOptions,
  statusListOptions,
  wrapRef,
} from "./store";
export const calcWrapHeight = () => {
  nextTick(() => {
    const distance = getTopDistance(wrapRef.value);
    wrapRef.value.style.height = `calc(100vh - ${distance}px - 10px - 62px)`;
  });
};
export const handleSubmit = async ({ loading, visible }: any) => {
  try {
    loading.value = true;
    if (operateType.value === "view") {
    }
    if (operateType.value === "add") {
      const res: any = await withdrawRuleApi.create({
        name: drawerFormState.name,
        amount_list: drawerFormState.amount_list?.join(","),
        comment: drawerFormState.comment,
        is_withdraw_amount_limit: drawerFormState.is_withdraw_amount_limit,
        handling_fee_percentage: drawerFormState.handling_fee_percentage,
        is_handling_fee: drawerFormState.is_handling_fee,
        max_withdraw_amount: drawerFormState?.max_withdraw_amount ?? "",
        max_withdraw_count: drawerFormState?.max_withdraw_count ?? "",
      });
      if (res.code != 200) return message.error(res.message);
      message.success($t("base.addSuccess"));
      fetchList();
    }
    if (operateType.value === "update") {
      const res: any = await withdrawRuleApi.update({
        id: drawerFormState.id,
        name: drawerFormState.name,
        amount_list: drawerFormState.amount_list?.join(","),
        comment: drawerFormState.comment,
        is_withdraw_amount_limit: drawerFormState.is_withdraw_amount_limit,
        handling_fee_percentage: drawerFormState.handling_fee_percentage,
        is_handling_fee: drawerFormState.is_handling_fee,
        max_withdraw_amount: drawerFormState?.max_withdraw_amount ?? "",
        max_withdraw_count: drawerFormState?.max_withdraw_count ?? "",
      });
      if (res.code != 200) return message.error(res.message);
      message.success($t("base.updateSucess"));
      fetchList();
    }
  } catch (e: any) {
    message.error(e.message);
  } finally {
    loading.value = false;
    visible.value = false;
  }
};

export const fetchSearchMap = async () => {
  const res = reactive<any>(await withdrawApi.quertySearchDicts());
  paymentMerchantOptions.value = objectToOptions(
    res.data.platformList,
    "number"
  );
  statusListOptions.value =
    res?.data?.statusList?.map?.((label: any, value: any) => ({
      label,
      value,
    })) ?? [];
};
